package com.ysg.binding.viewadapter;

import android.widget.CompoundButton;
import android.widget.Switch;
import androidx.databinding.BindingAdapter;

import com.ysg.binding.command.BindingCommand;

public class SwitchViewAdapter {
    /**
     * 设置开关状态
     */
    @BindingAdapter("bindChecked")
    public static void setSwitchState(Switch mSwitch, boolean isChecked) {
        mSwitch.setChecked(isChecked);
    }

    /**
     * Switch的状态改变监听
     */
    @BindingAdapter("onCheckedChangeCommand")
    public static void onCheckedChangeCommand(Switch mSwitch, BindingCommand<Boolean> listener) {
        mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (listener != null){
                    listener.execute(isChecked);
                }
            }
        });
    }
}
